<?php
namespace app\controller\admin\system\admin;

use app\BaseController;
use app\common\repositories\system\AdminRepository as repository;
use app\validate\admin\AdminEditValidate;
use app\validate\admin\AdminValidate;
use think\App;
use think\exception\ValidateException;

class Admin extends BaseController
{
    protected repository $repository;

    public function __construct(App $app, repository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    public function getList()
    {
        $where = $this->request->params(['keyword', 'date', 'status']);
        [$page, $limit] = $this->page();
        return app('json')->success($this->repository->getList($where, $page, $limit));
    }


    public function switchStatus($id)
    {
        $status = $this->request->param('status');
        if (!$this->repository->exists($id))
            return app('json')->fail('数据不存在');
        $this->repository->update($id, ['status' => $status == 1 ? 1 : 0]);
        return app('json')->success('编辑成功');
    }


    public function createForm()
    {
        return app('json')->success(formToData($this->repository->form()));
    }

    public function updateForm($id)
    {
        if (!$this->repository->exists($id))
            return app('json')->fail('数据不存在');
        return app('json')->success(formToData($this->repository->updateForm($id)));
    }

    public function passwordForm($id)
    {
        if (!$this->repository->exists($id))
            return app('json')->fail('数据不存在');
        return app('json')->success(formToData($this->repository->passwordForm($id)));
    }


    public function create()
    {
        if(request()->adminId() != 1){
            throw new ValidateException('无权限');
        }

        $data = $this->request->params(['account', 'password','real_name']);
       // $validate->check($data);
        if(!$data['account'])
            throw new ValidateException('请输入账号');
        if(!$data['password'])
            throw new ValidateException('请输入密码');
        if(!$data['real_name'])
            throw new ValidateException('请输入店铺名称');

        if ($this->repository->fieldExists('account', $data['account']))
            return app('json')->fail('账号已存在');
        $data['pwd'] = $this->repository->passwordEncode($data['password']);
        $data['roles'] = [];
        unset($data['password']);
        $this->repository->create($data);

        return app('json')->success('添加成功');
    }


    public function update($id)
    {
        if(request()->adminId() != 1){
            throw new ValidateException('无权限');
        }

        $data = $this->request->params(['account','real_name']);
        // $validate->check($data);
        if(!$data['account'])
            throw new ValidateException('请输入账号');
        if(!$data['real_name'])
            throw new ValidateException('请输入店铺名称');
        if ($this->repository->fieldExists('account', $data['account'], $id))
            return app('json')->fail('账号已存在');
        $this->repository->update($id, $data);

        return app('json')->success('编辑成功');
    }


    public function password($id)
    {
        if(request()->adminId() != 1){
            throw new ValidateException('无权限');
        }

        $data = $this->request->params(['password']);

        if (!$this->repository->exists($id))
            return app('json')->fail('店铺不存在');
        $data['pwd'] = $this->repository->passwordEncode($data['password']);
        unset($data['password']);
        $this->repository->update($id, $data);

        return app('json')->success('修改密码成功');
    }

    public function delete($id)
    {
        if(request()->adminId() != 1 || $id == 1){
            throw new ValidateException('无权限');
        }

        if (!$this->repository->exists($id))
            return app('json')->fail('数据不存在');
        $this->repository->update($id, ['is_del' => 1]);
        return app('json')->success('删除成功');
    }


    public function edit(AdminEditValidate $validate)
    {
        $data = $this->request->params(['real_name', 'phone']);
        $validate->check($data);
        $this->repository->update($this->request->adminId(), $data);
        return app('json')->success('修改成功');
    }


    public function editForm()
    {
        $adminInfo = $this->request->adminInfo();
        return app('json')->success(formToData($this->repository->editForm(['real_name' => $adminInfo->real_name, 'phone' => $adminInfo->phone])));
    }


    public function editPassword(AdminValidate $validate)
    {
        $data = $this->request->params(['pwd', 'againPassword']);
        $validate->isPassword()->check($data);

        if ($data['pwd'] !== $data['againPassword'])
            return app('json')->fail('两次密码输入不一致');
        $data['pwd'] = $this->repository->passwordEncode($data['pwd']);
        unset($data['againPassword']);
        $this->repository->update($this->request->adminId(), $data);

        return app('json')->success('修改密码成功');
    }


    public function editPasswordForm()
    {
        return app('json')->success(formToData($this->repository->passwordForm($this->request->adminId(), true)));
    }

}
